package com.monkey.demo.service.impl;

import com.monkey.bean.Result;
import com.monkey.demo.entity.dto.UserDto;
import com.monkey.demo.entity.po.UserPo;
import com.monkey.demo.dao.UserMapper;
import com.monkey.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public int addOrUpdateUser(UserDto userDto) {
        UserPo userPo = new UserPo();
        BeanUtils.copyProperties(userDto,userPo);
        if(userDto.getId()==null){
            return userMapper.insert(userPo);
        }else {
            return userMapper.updateById(userPo);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delUserById(Long userId) {
        int i = userMapper.deleteById(userId);
        return Result.ok(i);
    }
}
